package com.timemaneger.thinkintime;

import android.content.Intent;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.timemaneger.thinkintime.GridView.BookShelf;

import java.io.File;

public class ShelfActivity extends AppCompatActivity {

    private String path;
    private File[] subFile;
    private static int AddBook=100;
    private BookShelf bookShelf;
    private boolean isShowDelete=false;
    private int[] bookdate={R.drawable.add,R.mipmap.book1,R.mipmap.book2,R.mipmap.book3,R.mipmap.book4,R.mipmap.book5,};
    private String []name={"新建","book1","book2","book3","book4","book5"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shelf);
        bookShelf=(BookShelf) findViewById(R.id.book_shelf);
        init();
    }

    private void init() {
        initBookDate();
        final ShelfAdapter adapter=new ShelfAdapter();
        bookShelf.setAdapter(adapter);
        bookShelf.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                isShowDelete=!isShowDelete;
                adapter.setIsShowDelete(isShowDelete);
                return true;
            }
        });
        bookShelf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                if(isShowDelete){
                    if(position>=bookdate.length){

                    }else{
                        if(position==0){
                            //添加书册
                            Intent intent=new Intent(ShelfActivity.this,AddBookActivity.class);
                            startActivityForResult(intent,AddBook);
                        }else {
                            Handler handler=new Handler();
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    isShowDelete=!isShowDelete;
                                    String title=subFile[position-1].getName();
                                    File file=new File(path,title);
                                    File [] fileArray=file.listFiles();
                                    for(File f:fileArray){
                                        f.delete();
                                    }
                                    file.delete();
                                    init();
                                }
                            });
                        }
                    }
                }else {
                    if(position>=bookdate.length){

                    }else{
                        if(position==0){
                            //添加书册
                            Intent intent=new Intent(ShelfActivity.this,AddBookActivity.class);
                            startActivityForResult(intent,AddBook);
                        }else {
                            Handler handler=new Handler();
                            handler.post(new Runnable() {
                                @Override
                                public void run() {

                                    String title=subFile[position-1].getName();
                                    Intent intent=new Intent(ShelfActivity.this,NoteBookActivity.class);
                                    intent.putExtra("path",path+"/"+title);
                                    startActivity(intent);
                                }
                            });
                        }
                    }
                }

            }
        });
    }

    private void initBookDate() {
        path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/Html";
        File dir=new File(path);
        if(dir.exists()){

        }else {
            dir.mkdir();
        }
        subFile=dir.listFiles();
        int len=subFile.length;
        bookdate=new int[len+1];
        name=new String[len+1];
        bookdate[0]=R.drawable.add;
        name[0]="新建";
        for(int i=1;i<=len;i++){
            String title=subFile[i-1].getName();
            String str[]=title.split("-");
            String bookName=str[0];
            String bg=str[1];
            bookdate[i]=Integer.valueOf(bg);
            name[i]=bookName;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==AddBook){
            if(resultCode==RESULT_CANCELED){

            }else if(resultCode==RESULT_OK){
                init();
            }
        }
    }

    class ShelfAdapter extends BaseAdapter{
        private boolean isShowDelete=false;
        @Override
        public int getCount() {
            return bookdate.length;
        }

        public void setIsShowDelete(boolean isShowDelete){
            this.isShowDelete=isShowDelete;
            notifyDataSetChanged();
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView= LayoutInflater.from(getApplicationContext()).inflate(R.layout.book_item,null);
            TextView view=(TextView) convertView.findViewById(R.id.book_item_title);
            ImageView delete_book=(ImageView) convertView.findViewById(R.id.delete_book);
            if(bookdate.length>position){
                delete_book.setVisibility(isShowDelete?View.VISIBLE:View.INVISIBLE);
                if(position==0){
                    delete_book.setVisibility(View.INVISIBLE);
                }
                if(position<name.length){
                    view.setText(name[position]);
                }
                view.setBackgroundResource(bookdate[position]);
            }else {
                view.setBackgroundResource(bookdate[0]);
                view.setClickable(false);
                view.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }
    }
}
